package com.easycamera.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.easycamera.R
import com.easycamera.adapter.FilterAdapter.FilterHolder
import com.easycamera.helper.FilterTypeHelper.FilterType2Color
import com.easycamera.helper.FilterTypeHelper.FilterType2Name
import com.easycamera.helper.FilterTypeHelper.FilterType2Thumb
import com.seu.magicfilter.filter.helper.MagicFilterType

class FilterAdapter(private val context: Context, private val filters: Array<MagicFilterType>?) : RecyclerView.Adapter<FilterHolder>() {
    private var selected = 0
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FilterHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.filter_item_layout,
                parent, false)
        val viewHolder = FilterHolder(view)
        viewHolder.thumbImage = view
                .findViewById<View>(R.id.filter_thumb_image) as ImageView
        viewHolder.filterName = view
                .findViewById<View>(R.id.filter_thumb_name) as TextView
        viewHolder.filterRoot = view
                .findViewById<View>(R.id.filter_root) as FrameLayout
        viewHolder.thumbSelected = view
                .findViewById<View>(R.id.filter_thumb_selected) as FrameLayout
        viewHolder.thumbSelected_bg = view.findViewById(R.id.filter_thumb_selected_bg)
        return viewHolder
    }

    override fun onBindViewHolder(holder: FilterHolder, position: Int) {
        holder.thumbImage!!.setImageResource(FilterType2Thumb(filters!![position]))
        holder.filterName!!.setText(FilterType2Name(filters[position]))
        holder.filterName!!.setBackgroundColor(context.resources.getColor(
                FilterType2Color(filters[position])))
        if (position == selected) {
            holder.thumbSelected!!.visibility = View.VISIBLE
            holder.thumbSelected_bg!!.setBackgroundColor(context.resources.getColor(
                    FilterType2Color(filters[position])))
            holder.thumbSelected_bg!!.alpha = 0.7f
        } else {
            holder.thumbSelected!!.visibility = View.GONE
        }
        holder.filterRoot!!.setOnClickListener(View.OnClickListener {
            if (selected == position) return@OnClickListener
            val lastSelected = selected
            selected = position
            notifyItemChanged(lastSelected)
            notifyItemChanged(position)
            onFilterChangeListener!!.onFilterChanged(filters[position])
        })
    }

    override fun getItemCount(): Int {
        return filters?.size ?: 0
    }

    inner class FilterHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
        var thumbImage: ImageView? = null
        var filterName: TextView? = null
        var thumbSelected: FrameLayout? = null
        var filterRoot: FrameLayout? = null
        var thumbSelected_bg: View? = null
    }

    interface OnFilterChangeListener {
        fun onFilterChanged(filterType: MagicFilterType?)
    }

    private var onFilterChangeListener: OnFilterChangeListener? = null
    fun setOnFilterChangeListener(onFilterChangeListener: OnFilterChangeListener) {
        this.onFilterChangeListener = onFilterChangeListener
    }

}